#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string>

using namespace std;

const char *pathname="/home/zzn";
const int proj_id=0x66;

string toHex(key_t key)
{
    char buffer[1024];
    snprintf(buffer, sizeof(buffer), "0x%x", key);
    return buffer;
}

// 1.获得共享内存的key
key_t GetShmKey()
{
    key_t key=ftok(pathname,proj_id);
    if(key==-1)
    {
        perror("1.GetKey:tofok");
        exit(1);
    }
    return key;
}

// 2.创建共享内存的id
int CreatShmId(key_t key,size_t size,int flag)
{
    int shmid=shmget(key,size,flag);
    if(shmid<0)
    {
        perror("2.CreatShmId:shmget");
        exit(2);
    }
    return shmid;
}

// 3.删除共享内存
void DeleteShm(int shmid)
{
    int n=shmctl(shmid,IPC_RMID,nullptr);
    if(n<0)
    {
        perror("3.DeleteShm:shmctl");
        exit(3);
    }
    else
    {
        cout<<"shmctl delete shm success,shmid:"<<shmid<<endl;
    }
}